//
//  RecommendVC.m
//  Project
//
//  Created by liting on 2025/9/4.
//


#import "RecommendVC.h"
#import "RecommendCell.h"
#import "HomeRepository.h"
#import "RecommendModel.h"
#import "FlutterWrapperVC.h"

static NSString *const kRecommendCellName = @"RecommendCell";

@interface RecommendVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, copy) NSArray<RecommendModel *> *dataArray;

@end

@implementation RecommendVC

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
    [self loadNewData];
}

#pragma mark - 初始化
- (void)setupUI {
    [self setNavTitle:@"推荐列表"];

    [self setupTableViewWithStyle:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    [self.tableView registerClass:[RecommendCell class] forCellReuseIdentifier:kRecommendCellName];
    [self addRefreshHeaderWithScrollView:self.tableView];

    [self setupNativeLabel];
}

#pragma mark - 网络请求
- (void)loadNewData {
    self.dataArray = [[HomeRepository sharedLoader]getRecommendListData];
    [self.tableView reloadData];
    [self endRefreshing];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:kRecommendCellName forIndexPath:indexPath];
    RecommendModel *model = self.dataArray[indexPath.row];
    [cell configureWithModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    FlutterWrapperVC *flutterVC = [[FlutterWrapperVC alloc] initWithRoute:@"/commits" params:@{} navigationBarHidden:YES];
    flutterVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:flutterVC animated:YES];
}

@end
